<?php

namespace App\Admin\Traits;

use App\Admin\Services\GrantService;
use App\Models\AdminUser;
use Auth;
use Route;

trait RouteHelper
{
    /**
     * 獲取系統所有路由
     * @return mixed
     */
    public function getRoutes()
    {
        // 獲取所有路由,僅僅過濾後台域名
        $routes = Route::getRoutes();
        $domain = config('admin.route.domain');
        $pathes = [
            [
                'name'        => '*',
                'path'        => '*',
                'description' => '超級權限',
            ],
        ];
        foreach ($routes as $k => $value) {
            if (!isset($value->action['domain']) || $value->action['domain'] != $domain) {
                continue;
            }
            $pathes[] = [
                'path'        => $value->methods[0] . ':' . $value->uri,
                'name'        => $value->getName(),
                'description' => $value->getActionDescription(),
            ];
        }
        return $pathes;
    }

    /**
     * 獲取當前登錄用户擁有的路由
     */
    public function getHasRoutes()
    {
        $admin = Auth::guard('admin')->user();
        if (!$admin instanceof AdminUser) {
            return [];
        }
        $this->grantService = new GrantService();
        return $this->grantService->getRoutes($admin);
    }
}
